﻿#region license
//------------------------------------------------------------------------------
// <copyright company="Ascertaint.org">
//     Copyright (c) Ascertaint.org.  All rights reserved.
// </copyright> 
//
// Authors: Ming Tsai (ming@ascertaint.org)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------
#endregion

using System;
using System.Text.RegularExpressions;
using System.Web;
using Ums.CellPhoneInfo;

namespace Ums.CellPhoneInfo {
    /// <summary>
    /// 中國門號說明
    /// ** 格式: +86XYABCDEFGHIK 或 86XYABCDEFGHIJK
    /// ** 規則 
    /// (1) X=0   (小靈通) 長度:11 or 12 (不含國碼)
    /// 如果為小靈通則需要加區碼才能撥通。例：打到北京，北京的區碼是010，則撥號方式為輸入+8610以及後八碼。
    /// (註：區碼的第一個0無需撥打。) 
    /// ex.: +86 013 42477688 | 86 072 776633852
    /// 
    /// (2) X=1  (移動 & 聯通) => Y=3 or 5 長度:11 
    /// ex.: 86 13 82699-5120 | +86 15 91243-1217
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class ChinaCellPhoneParser : CellPhoneParser {
        /// <summary>country phone pattern</summary>
        public static string PhonePattern = "^86|^8613|^8615|^860|^13|^15";

        protected override bool IsValid(string num) {
            CellPhoneNumber o = ParseExact(num);
            int test = o.AreaCode.Length + o.Prefix.Length + o.PhoneNumber.Length;
            if (test != 11 || test != 12)
                return false;
            else
                return true;
        }

        protected override CellPhoneNumber ParseExact(string numToParse) {
            string num = HttpUtility.UrlDecode(numToParse);
            num = StriptOffNonNumeric(num);

            CellPhoneNumber o = new CellPhoneNumber();
            o.CountryIsoCode = "cn";
            o.CountryCode = "86";

            string result = Regex.Replace(num, "^86", "");

            //小靈通
            if (Regex.IsMatch(result, "^0")) {
                result = Regex.Replace(result, "^0", "");
                o.AreaCode = result.Substring(0, 2);

                result = Regex.Replace(result, "^" + o.AreaCode, "");
                o.Prefix = result.Substring(0, 5);

                result = Regex.Replace(result, "^" + o.PhoneNumber, "");
                o.PhoneNumber = result;

                int len = o.AreaCode.Length + o.Prefix.Length + o.PhoneNumber.Length;
                if (len == 10 || len == 11)
                    o._isValidNumber = true;
            }
            //(移動 & 聯通)
            else if (Regex.IsMatch(result, "^13")) {
                //002 8613 53266-5001
                o.AreaCode = "13";
                result = Regex.Replace(result, "^" + o.AreaCode, "");
                o.Prefix = result.Substring(0, 5);

                result = Regex.Replace(result, "^" + o.PhoneNumber, "");
                o.PhoneNumber = result;

                int len = o.AreaCode.Length + o.Prefix.Length + o.PhoneNumber.Length;
                if (len == 11)
                    o._isValidNumber = true;
            }                
            else if (Regex.IsMatch(result, "^15")) {
                o.AreaCode = "15";
                result = Regex.Replace(result, "^" + o.AreaCode, "");
                o.Prefix = result.Substring(0, 5);

                result = Regex.Replace(result, "^" + o.PhoneNumber, "");
                o.PhoneNumber = result;

                int len = o.AreaCode.Length + o.Prefix.Length + o.PhoneNumber.Length;
                if (len == 11)
                    o._isValidNumber = true;
                else
                    o._isValidNumber = false;
            }
            else {
                throw new Exception("phone prefix not found - china number must have 13, 15, or 0xx as prefix.");
            }

            result = Regex.Replace(result, "^" + o.Prefix, "");
            o.PhoneNumber = result;

            o._isValidNumber = true;
            return o;
        }
    }
}
